Script Resource\Help

I am tring to read a text file and for each line insert as a variable.I am new to PS and have used batch files in the past to do this . I am trying to better understand PS and would like know how best to achieve the below requirement.

I have tried various "for" statements

Example:

$variable1 = get-content d:\file.txt

$variable2 = get-content d:\files2.txt

<# the below should run for each line in the txt files #>

$user = get-spuser -identity $variable1

$user.Displayname = "$variable2"

$user.update()

Any insight is greatly appreciated

Regards,

Dennis

June 30th, 2015 2:07pm

help for

help foreach

help ForEach-Object

Free Windows Admin Tool Kit Click here and download it now
June 30th, 2015 2:29pm

Hi Dennis,

I assume you save the username and Displayname in different text files, and you want to set displayname with the cmdlets "Set-SPUser", I recommend it's better to save the username and Displayname as a csv file.

If you have confirmed the usernames, please remove the parameter "-whatif" in the "Set-SPUser" cmdlet:

$variable1 = get-content C:\anna\user.txt
$variable2 = get-content C:\anna\displayname.txt
$count=if($variable1.count -gt $variable2.count){$variable2.count} else{$variable1.count}
for($i=0;$i -lt $count;$i++){
Set-SPUser -Identity $variable1[$i] -DisplayName $variable2[$i] -WhatIf
}

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Ann

July 1st, 2015 6:04am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics